
#include <stdlib.h>
#include <stdarg.h>

#include "Log.h"

#if LOG_TIME_ENABLE

#include "FreeRTOS.h"
#include "task.h"
TickType_t xTaskGetTickCount( void );
unsigned int Log_Time(void){
	return xTaskGetTickCount();
}



#endif

int SEGGER_RTT_vprintf(unsigned BufferIndex, const char * sFormat, va_list * pParamList);

void Log_Buf(uint8_t ch, const char* color, const uint8_t* buf, uint32_t len, uint8_t align, const char* sFormat, ...){
	uint32_t i;
	va_list ParamList;
	if(color){
		DBG_CH_WRITE(ch, color);
	}
	if(sFormat){
		va_start(ParamList, sFormat);
		SEGGER_RTT_vprintf(ch, sFormat, &ParamList);
		va_end(ParamList);
	}
	DBG_CH_PRINT(ch, "(%u)", len);
	for(i = 0; i < len; i++){
		if(i % align == 0){
			DBG_CH_WRITE(ch, "\r\n");
		}
		DBG_CH_PRINT(ch, "%02X ", buf[i]);
	}
	DBG_CH_WRITE(ch, "\r\n");
}

void Log_Head(uint8_t ch, const char* color, const char* tag){
	if(color){
		DBG_CH_WRITE(ch, color);
	}
	if(tag){
		DBG_CH_WRITE(ch, tag);
		DBG_CH_WRITE(ch, " ");
	}
}

int Log_Log(uint8_t ch, const char* color, const char* tag, const char* sFormat, ...){
	int r;
	va_list ParamList;
	if(color){
		DBG_CH_WRITE(ch, color);
	}
	if(tag){
		DBG_CH_WRITE(ch, tag);
	}
	va_start(ParamList, sFormat);
	r = SEGGER_RTT_vprintf(ch, sFormat, &ParamList);
	va_end(ParamList);
	return r;
}

